package org.zjx.db;

import org.zjx.model.game.BuyItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuyItemDB {
    private static boolean isTableCreated = false;
    private final SQLiteHelper dbHelper;

    public BuyItemDB() {
        dbHelper = SQLiteHelper.getSingleton();
        if (!isTableCreated) {
            createTable();
            isTableCreated = true;
        }
    }

    public void createTable() {
        String createTable = "CREATE TABLE IF NOT EXISTS buy_item (\n"
                + "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
                + "    itemName TEXT NOT NULL,\n"
                + "    npcId INTEGER NOT NULL\n"
                + ");";
        dbHelper.createTable(createTable);
    }

    public void insert(BuyItem buyItem) {
        String sql = "INSERT INTO buy_item(itemName, npcId) VALUES(?, ?)";
        Object[] params = {
                buyItem.getItemName(),
                buyItem.getNpcId()
        };
        dbHelper.executeUpdate(sql, params);
    }

    public void update(BuyItem buyItem) {
        String sql = "UPDATE buy_item SET itemName = ?, npcId = ? WHERE id = ?";
        Object[] params = {
                buyItem.getItemName(),
                buyItem.getNpcId(),
                buyItem.getId()
        };
        dbHelper.executeUpdate(sql, params);
    }

    public void deleteById(int id) {
        String sql = "DELETE FROM buy_item WHERE id = ?";
        Object[] params = {id};
        dbHelper.executeUpdate(sql, params);
    }

    public BuyItem findById(int id) {
        String sql = "SELECT * FROM buy_item WHERE id = ?";
        Object[] params = {id};
        List<BuyItem> buyItems = dbHelper.executeQuery(sql, params, BuyItem.class);
        return buyItems.isEmpty() ? null : buyItems.get(0);
    }

    public BuyItem findByItemName(String itemName) {
        String sql = "SELECT * FROM buy_item WHERE itemName = ?";
        Object[] params = {itemName};
        List<BuyItem> buyItems = dbHelper.executeQuery(sql, params, BuyItem.class);
        return buyItems.isEmpty() ? null : buyItems.get(0);
    }

    public List<BuyItem> findByNpcId(int npcId) {
        String sql = "SELECT * FROM buy_item WHERE npcId = ?";
        Object[] params = {npcId};
        return dbHelper.executeQuery(sql, params, BuyItem.class);
    }

    public BuyItem findByItemNameAndNpcId(String itemName, int npcId) {
        String sql = "SELECT * FROM buy_item WHERE itemName = ? AND npcId = ?";
        Object[] params = {itemName, npcId};
        List<BuyItem> buyItems = dbHelper.executeQuery(sql, params, BuyItem.class);
        return buyItems.isEmpty() ? null : buyItems.get(0);
    }

    public List<BuyItem> findAll() {
        String sql = "SELECT * FROM buy_item";
        return dbHelper.executeQuery(sql, new Object[]{}, BuyItem.class);
    }

    public Map<String, Object> findAllWithPagination(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        String sql = "SELECT * FROM buy_item LIMIT ? OFFSET ?";
        List<BuyItem> buyItems = dbHelper.executeQuery(sql, new Object[]{pageSize, offset}, BuyItem.class);

        String countSql = "SELECT COUNT(*) FROM buy_item";
        int totalRecords = dbHelper.executeQuery(countSql, new Object[]{}, Integer.class)
                .stream().findFirst().orElse(0);

        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("buyItemList", buyItems);
        result.put("totalRecords", totalRecords);
        result.put("totalPages", totalPages);
        result.put("currentPage", pageNum);

        return result;
    }

    public void clearData() {
        dbHelper.clearData("buy_item");
    }

    public void dropTable() {
        dbHelper.dropTable("buy_item");
    }
}